Conditions | 5 |
Total Lines | 24 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 11 |
CRAP Score | 5 |
Changes | 0 |
1 | /** |
||
2 | * Function to sort JSON data. |
||
3 | * @param {unknown} data - The data to be sorted. |
||
4 | * @param {boolean} [asc=true] - Whether to sort in ascending order. |
||
5 | * @returns {unknown} The sorted data. |
||
6 | */ |
||
7 | 1 | export function sort(data: unknown, asc: boolean = true): unknown { |
|
8 | 105 | if (!data || typeof data !== 'object') { |
|
9 | 61 | return data; |
|
10 | } |
||
11 | |||
12 | 44 | if (Array.isArray(data)) { |
|
13 | 27 | return data.map(item => sort(item, asc)); |
|
14 | } |
||
15 | |||
16 | 35 | if (isNonSortableObject(data)) { |
|
17 | 11 | return data; |
|
18 | } |
||
19 | |||
20 | 24 | const sortedEntries = Object.entries(data as Record<string, unknown>) |
|
21 | 52 | .sort(([a], [b]) => asc ? a.localeCompare(b) : b.localeCompare(a)); |
|
22 | |||
23 | 24 | return Object.fromEntries( |
|
24 | 60 | sortedEntries.map(([key, value]) => [key, sort(value, asc)]) |
|
25 | ); |
||
42 |